< previous page page_683 next page >

Page 683
therefore eliminate the gathering and converting to SQL steps by writing the UPDATE statement yourself. In addition, you dont have to bring the data back to the client each time you move through the loop. Instead, you just send the SQL down the server to be executed. This way, no data comes back to the client across the network. This is much faster because theres reduced network traffic.
In general, you should strive to use SQL whenever possible. For example, if you find yourself writing a loop in Visual Basic just to calculate a sum, average, count, minimum, or maximum, you should change the code to use the SQL aggregate functionSum(), Avg(), Count(), Min(), or Max()instead.
Forward Only Versus Keyset
When youre opening a recordset, its always advisable to use a forward-only recordset instead of any other cursor type. A forward-only recordset will open about 25 percent faster against a Jet database and over 85 percent faster against a database server. Therefore, you should avoid opening an ADO recordset with the following:
9f31265bb40b592b1929e18a47ab28e5.gif
oRS.Open strSQL, _
         oConn, _
         adOpenKeyset, _
         adLockPessimistic
Instead, you should use the following recordset type:
9f31265bb40b592b1929e18a47ab28e5.gif
oRS.Open strSQL, _
         oConn, _
         adOpenForwardOnly, _
         adLockReadOnly
Of course this means you need to use SQL to perform any updates, because a forward-only recordset cant be updated with most drivers. However, as mentioned earlier, using SQL is much faster anyway.
The biggest reason this is so much faster has to do with what happens when you open a keyset type. First, ADO has to run the SQL string. Next, it queries the information about each of the columns returned. ADO retrieves the size and data type of each column as well as the column type. ADO also has to retrieve the list of primary key field(s) from the result set. Next, it loops through the resultset and grabs the values of the primary key fields to create bookmarks. It then goes back to the top of the resultset and starts reading the bookmarks to grab each row of data.

 
< previous page page_683 next page >

If you like this book, buy it!